// 重置
function reset() {
  $(".filter-item").removeClass("active");
  $(".filter-list .filter-item:first-child").addClass("active");
}
// 完成
function submit() {
  console.log($(".filter-list.cate .active p").html());
  console.log($(".filter-list.area .active p").html());
  closePopup("popupFilter");
}

// 选择选项
$(".filter-list").on("click", ".filter-item", function () {
  // 单选
  $(this).siblings().removeClass("active");
  $(this).addClass("active");
});

// 打开弹窗
function openPopup(id) {
  $("#" + id).show();
  $("body").css("overflow", "hidden").css("height", "100vh");
}
// 关闭客服弹窗
function closePopup(id) {
  $("#" + id).hide();
  $("body").css("overflow", "auto").css("height", "auto");
}

// 页面滚动到底部加载更多
let loading = false; // 是否正在加载中
let isEnd = false; // 是否已经加载完所有数据
$(window).on("scroll", function () {
  let scrollTop = $(this).scrollTop();
  let scrollHeight = $(document).height();
  let windowHeight = $(this).height();
  if (scrollTop + windowHeight >= scrollHeight - 100) {
    if (loading || isEnd) {
      return;
    }
    console.log("加载更多数据");
    loading = true;
    $("#textTip").text("数据加载中...");
    setTimeout(function () {
      loadMoreData();
      loading = false;
    }, 1000); // 模拟加载时间
  }
});

// 加载更多数据
function loadMoreData() {
  if ($(".item").length > 12) {
    // 模拟没有更多数据
    $("#textTip").text("没有更多数据了");
    isEnd = true;
  } else {
    // 模拟加载更多数据
    for (let i = 0; i < 6; i++) {
      let $item = $(`
         <div class="item">
          <div class="item-content">
            <div class="text-name ellipsis">
              河北中废通网络技术有限公司河北中废通网络技术有限公司
            </div>
            <div class="text-vip flex">
              <img src="./images/vip-zx.png" class="img-vip" />
              <p>第<span class="text-green">5</span>年</p>
            </div>
            <div class="icon-list flex">
              <div class="icon-item flex">
                <img src="./images/icon-shiming.png" class="icon" />
                <span class="text">实名认证</span>
              </div>
              <div class="icon-item flex">
                <img src="./images/icon-shidi.png" class="icon" />
                <span class="text">实地认证</span>
              </div>
            </div>
            <div class="line"></div>
            <div class="text-desc ellipsis">
              所属行业:
              智能回收、科技环保、智能回收、科技环保、智能回收、科技环保
            </div>
            <div class="text-desc ellipsis">公司主营: 废金属、废km电、废</div>
            <a href="https://www.feijiu.net" class="button primary">
              在线询价
            </a>
          </div>
        </div>
      `);
      $(".list").append($item);
    }
  }
}

// 在线询盘
function openMessagePopup(uid) {
  form.uid = uid;
  openPopup("popupMessage");
}
// 更新输入框字数
function updateTextCount() {
  const textarea = document.getElementById("desc");
  const countElement = document.getElementById("descCount");
  const maxLength = 200;
  const currentLength = textarea.value.length;
  countElement.textContent = currentLength;
  if (currentLength > maxLength) {
    textarea.value = textarea.value.substring(0, maxLength);
    countElement.textContent = maxLength;
  }
}

// 提交留言
let form = {
  uid: "",
  name: "",
  phone: "",
  desc: "",
};
let rule = {
  name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  phone: [
    { required: true, message: "请输入手机号", trigger: "blur" },
    {
      pattern: /^1[3456789]\d{9}$/,
      message: "请输入正确的手机号",
      trigger: "blur",
    },
  ],
  desc: [{ required: true, message: "请输入补充内容", trigger: "blur" }],
};
let loadingSubmit = false;
function onSubmitMessage() {
  if (loadingSubmit) {
    return;
  }
  if (validate()) {
    loadingSubmit = true;
    console.log(form);
    showToast("提交成功");
    setTimeout(() => {
      closePopup("popupMessage");
      loadingSubmit = false;
    }, 1000);
  }
}
function validate() {
  form.name = $("input[name='name']").val().trim();
  form.phone = $("input[name='phone']").val().trim();
  form.desc = $("textarea[name='desc']").val().trim();
  let rersult = true;
  for (let key in rule) {
    rule[key].forEach((item) => {
      if (rersult) {
        if (item.required && !form[key].trim()) {
          showToast(item.message);
          rersult = false;
        }
        if (item.pattern && !item.pattern.test(form[key])) {
          showToast(item.message);
          rersult = false;
        }
      }
    });
  }
  return rersult;
}

// toast
let timer = null;
function showToast(message) {
  const toast = document.getElementById("toast");
  if (timer) {
    clearTimeout(timer);
    toast.hidden = true;
    timer = null;
  }
  toast.textContent = message;
  toast.hidden = false;
  timer = setTimeout(() => {
    toast.hidden = true;
    timer = null;
  }, 3000);
}
